<template>
  <div class="big">
    <!-- 头部返回 -->
    <div class="header">
      <van-nav-bar title="京东登录" left-text="返回" left-arrow @click-left="$router.go(-1)"/>
    </div>
    <!-- 表单登录 -->
    <div class="login">
    <van-form @submit="onSubmit">
    <van-cell-group inset>
    <van-field
      v-model="username"
      name="用户名"
      label="用户名"
      placeholder="用户名"
      :rules="[{ validator, message: '请正确填写用户名' }]"/>

    <van-field
      v-model="password"
      type="password"
      name="密码"
      label="密码"
      placeholder="密码"
      :rules="[{ pattern, message: '请填写密码' }]" />

  </van-cell-group>
  <div style="margin: 16px;">
    <van-button round block type="primary" native-type="submit">
      提交
    </van-button>
  </div>
</van-form>
    </div>
 
 <!-- 跳转注册界面 -->
  <div class="register">
   <p>去注册</p>
 <van-cell title="快速注册" is-link to="/register/step1"/>
  </div>
  </div>
</template>
<script setup lang="ts">
import { Dialog } from 'vant';
import {useRouter}from "vue-router"
import {useuserstore} from "../store/userstore"
import{loginapi} from "../api/user"
import{ref} from 'vue'
const validator = (val:any) =>/^1[3456789]\d{9}$/.test(val);
const pattern=/^(?!([a-zA-Z\d]*|[\d!@#\$%_\.]*|[a-zA-Z!@#\$%_\.]*)$)[a-zA-Z\d!@#\$%_\.]{6,16}$/
const router = useRouter()
let userstore=useuserstore()
 const username = ref("")
 const password = ref("")
 interface dd{
   loginname: string,
   password: string
 }
 const onSubmit=()=>{
  const value:dd={
   loginname: username.value,
   password: password.value
  }
  loginapi(value).then((res:any)=>{
     if (res.code == '10010') {
            Dialog.confirm({
                title: '提示',
                message: '用户未注册,请先注册!',
            }).then(() => {
                // 点击确定,跳转到注册页
                // console.log(useRouter);
                router.push('/register')
            }).catch(() => {
            });
        } else if (res.code == 200) {
            // 将用户信息存到pinaia 中
            userstore.setuserid(res.data.userid)
            localStorage.setItem("token",res.data.token)
            localStorage.setItem("userid",res.data.userid)
            // 跳转到首页
             router.push('/home');
  }
 })
 }
</script>
<style scoped lang="scss">
.login,.register{
  margin-top: 30px;
}
.register{
  p{
    margin-left: 15px;
  }
  .van-cell{
    margin-top:0px;
  }
}
</style>